home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / graphic / frasr182.zip / TARGA.C < prev    next >
C/C++ Source or Header  |  1993-02-18  |  20KB  |  776 lines

  1. /** targa.c **/
  2.  
  3. #ifdef __TURBOC__
  4. #    pragma    warn -par
  5. #endif
  6.  
  7. #define TARGA_DATA
  8.  
  9. #include    <stdio.h>
  10. #include    <stdlib.h>
  11. #include    <string.h>
  12. #ifndef XFRACT
  13. #include    <conio.h>
  14. #endif
  15. #include    "targa.h"
  16. #include    "fractint.h"
  17. #include    "prototyp.h"
  18.  
  19.  
  20. /*************    ****************/
  21.  
  22. extern char far *mapdacbox;
  23.  
  24. /*************    ****************/
  25.  
  26. void    WriteTGA( int x, int y, int index );
  27. int    ReadTGA ( int x, int y );
  28. void    EndTGA    ( void );
  29. void    StartTGA( void );
  30. void    ReopenTGA( void );
  31.  
  32. /*************    ****************/
  33.  
  34. static unsigned _fastcall near Row16Calculate(unsigned,unsigned);
  35. static void    _fastcall near PutPix16(int,int,int);
  36. static unsigned _fastcall near GetPix16(int,int);
  37. static unsigned _fastcall near Row32Calculate(unsigned,unsigned);
  38. static void    _fastcall near PutPix32(int,int,int);
  39. static unsigned _fastcall near GetPix32(int,int);
  40. static void    _fastcall near DoFirstPixel(int,int,int);
  41. static void _fastcall fatalerror(char far *);
  42. static int  GetLine(int);
  43. static void _fastcall near SetDispReg(int,int);
  44. static int  VWait(void);
  45. static void _fastcall SetVBorder(int,int);
  46. static void _fastcall SetBorderColor(long);
  47. static void _fastcall SetVertShift(int);
  48. static void _fastcall SetInterlace(int);
  49. static void _fastcall SetBlndReg(int);
  50. static void _fastcall SetRGBorCV(int);
  51. static void _fastcall SetVCRorCamera(int);
  52. static void _fastcall SetMask(int);
  53. static void _fastcall SetBlndReg(int);
  54. static void _fastcall SetContrast(int);
  55. static void _fastcall SetHue(int);
  56. static void _fastcall SetSaturation(int);
  57. static void _fastcall SetHBorder(int,int);
  58. static void SetFixedRegisters(void);
  59. static void _fastcall VCenterDisplay(int);
  60. static void _fastcall SetOverscan(int);
  61. static void _fastcall near TSetMode(int);
  62. static int  GraphInit(void);
  63. static void GraphEnd(void);
  64.  
  65. /*************    ****************/
  66.  
  67. int         xorTARGA;
  68. unsigned far *tga16 = NULL;  /* [256] */
  69. long     far *tga32;         /* [256] */
  70. static int   last = 0;
  71.  
  72. /*************    ****************/
  73.  
  74. extern int    sxdots,sydots;        /* # of dots on the physical screen */
  75. static int    initialized;
  76.  
  77. /*************    ****************/
  78.  
  79. static void    (near _fastcall *DoPixel) ( int x, int y, int index );
  80. static void    (near _fastcall *PutPixel)( int x, int y, int index );
  81. static unsigned (near _fastcall *GetPixel)( int x, int y );
  82.  
  83. /**************************************************************************/
  84. #ifdef __BORLANDC__
  85. #if(__BORLANDC__ > 2)
  86.    #pragma warn -eff
  87. #endif
  88. #endif
  89.  
  90. static unsigned _fastcall near Row16Calculate( unsigned line, unsigned x1 )
  91. {
  92.     outp( DESTREG, (line >> 5) );
  93.     return( ((line & 31) << 10) | (x1 << 1) ); /* calc the pixel offset */
  94. }
  95.  
  96. /**************************************************************************/
  97.  
  98. static void _fastcall near PutPix16( int x, int y, int index )
  99. {
  100. unsigned far * ip;
  101.  
  102.     /**************/
  103.     ip = MK_FP( MEMSEG, Row16Calculate( y, x ) );
  104.     if( ! xorTARGA )
  105.         *ip = tga16[index];
  106.     else
  107.         *ip = *ip ^ 0x7fff;
  108. }
  109.  
  110. /**************************************************************************/
  111.  
  112. static unsigned _fastcall near GetPix16( int x, int y )
  113. {
  114. register unsigned pixel, index;
  115. unsigned far * ip;
  116.     /**************/
  117.     ip = MK_FP( MEMSEG, Row16Calculate( y, x ) );
  118.     pixel = *ip & 0x7FFF;
  119.     if( pixel == tga16[last] ) return( last );
  120.     for( index = 0; index < 256; index++ )
  121.         if( pixel == tga16[index] ) {
  122.             last = index;
  123.             return( index );
  124.         }
  125.     return( 0 );
  126. }
  127.  
  128. /**************************************************************************/
  129.  
  130. static unsigned _fastcall near Row32Calculate( unsigned line, unsigned x1 )
  131. {
  132.     outp( DESTREG, (line >> 4) );
  133.     return ( ((line & 15) << 11) | (x1 << 2) ); /* calc the pixel offset */
  134. }
  135.  
  136. /**************************************************************************/
  137.  
  138. static void _fastcall near PutPix32( int x, int y, int index )
  139. {
  140. long far * lp;
  141.     lp = MK_FP( MEMSEG, Row32Calculate( y, x ) );
  142.     if( ! xorTARGA )
  143.         *lp = tga32[index];
  144.     else
  145.         *lp = *lp ^ 0x00FFFFFF;
  146. }
  147.  
  148. /**************************************************************************/
  149.  
  150. static unsigned _fastcall near GetPix32( int x, int y )
  151. {
  152. register int index;
  153. long    pixel;
  154. long    far * lp;
  155.  
  156.     lp = MK_FP( MEMSEG, Row32Calculate( y, x ) );
  157.     pixel = *lp & 0x00FFFFFF;
  158.     if( pixel == tga32[last] ) return( last );
  159.     for( index = 0; index < 256; index++ )
  160.         if( pixel == tga32[index] ) {
  161.             last = index;
  162.             return( index );
  163.         }
  164.     return( 0 );
  165. }
  166.  
  167. /**************************************************************************/
  168.  
  169. static void _fastcall near DoFirstPixel( int x, int y, int index )
  170. {
  171. int cnt;
  172.     TSetMode( targa.mode | 1 );
  173.     for( cnt = 0; cnt < targa.MaxBanks; cnt += 2 ) { /* erase */
  174.         outp( DESTREG, cnt );
  175.         outp( SRCREG,  cnt + 1 );
  176.         erasesegment(targa.memloc,0);  /** general.asm **/
  177.     }
  178.     TSetMode( targa.mode & 0xFFFE );
  179.     PutPixel = DoPixel;
  180.     (*PutPixel)( x, y, index );
  181. }
  182.  
  183. #ifdef __BORLANDC__
  184. #if(__BORLANDC__ > 2)
  185.    #pragma warn +eff
  186. #endif
  187. #endif
  188.  
  189. /***************************************************************************/
  190.  
  191. void WriteTGA( int x, int y, int index )
  192. {
  193.     OUTPORTB(MODEREG, targa.mode |= 1 );    /* TSetMode inline for speed */
  194.     (*PutPixel)( x, sydots-y, index&0xFF ); /* fix origin to match EGA/VGA */
  195.     OUTPORTB(MODEREG, targa.mode &= 0xFFFE );
  196. }
  197.  
  198. /***************************************************************************/
  199.  
  200. int ReadTGA( int x, int y )
  201. {
  202. int val;
  203.     OUTPORTB(MODEREG, targa.mode |= 1 );    /* TSetMode inline for speed */
  204.     val = (*GetPixel)( x, sydots-y );
  205.     OUTPORTB(MODEREG, targa.mode &= 0xFFFE );
  206.     return( val );
  207. }
  208.  
  209. /***************************************************************************/
  210.  
  211. void EndTGA( void )
  212. {
  213.     if( initialized ) {
  214.         GraphEnd();
  215.         initialized = 0;
  216.     }
  217. }
  218.  
  219. /***************************************************************************/
  220.  
  221. void StartTGA()
  222. {
  223. int    i;
  224. static char far couldntfind[]={"Could not find Targa card"};
  225. static char far noenvvar[]={"TARGA environment variable missing"};
  226. static char far insuffmem[]={"Insufficient memory for Targa"};
  227.  
  228.     /****************/
  229.     if( initialized ) return;
  230.     initialized = 1;
  231.  
  232.     /****************/
  233.     /* note that video.asm has already set the regualar video adapter */
  234.     /* to text mode (ax in Targa table entries is 3);          */
  235.     /* that's necessary because TARGA can live at 0xA000, we DO NOT   */
  236.     /* want to have an EGA/VGA in graphics mode!!              */
  237.     ReopenTGA(); /* clear text screen and display message */
  238.  
  239.     /****************/
  240.     /*** look for and activate card ***/
  241.     if ((i = GraphInit()))
  242.         fatalerror((i == -1) ? couldntfind : noenvvar);
  243.  
  244.     VCenterDisplay( sydots + 1 );
  245.  
  246.     if (tga16 == NULL)
  247.         if ( (tga16 = (unsigned far *)farmemalloc(512L)) == NULL
  248.           || (tga32 = (long     far *)farmemalloc(1024L)) == NULL)
  249.         fatalerror(insuffmem);
  250.  
  251.     SetTgaColors();
  252.  
  253.     if( targa.boardType == 16 ) {
  254.         GetPixel = GetPix16;
  255.         DoPixel = PutPix16;
  256.     }
  257.     else {
  258.         GetPixel = GetPix32;
  259.         DoPixel = PutPix32;
  260.     }
  261.     PutPixel = DoFirstPixel;    /* on first pixel --> erase */
  262.  
  263.     if( sydots == 482 ) SetOverscan( 1 );
  264.  
  265.     TSetMode( targa.mode & 0xFFFE );
  266.  
  267.     /****************/
  268.     if (mapdacbox == NULL && SetColorPaletteName("default") != 0)
  269.         exit( 1 ); /* stopmsg has already been issued */
  270.  
  271. }
  272.  
  273. void ReopenTGA()
  274. {
  275. static char far runningontarga[]={"Running On TrueVision TARGA Card"};
  276.     helptitle();
  277.     putstring(2,20,7,runningontarga);
  278.     movecursor(6,0); /* in case of brutal exit */
  279. }
  280.  
  281. static void _fastcall fatalerror(char far *msg)
  282. {
  283. static char far abortmsg[]={"...aborting!"};
  284.     putstring(4,20,15,msg);
  285.     putstring(5,20,15,abortmsg);
  286.     movecursor(8,0);
  287.     exit(1);
  288. }
  289.  
  290.  
  291.  
  292. /***  the rest of this module used to be separate, in tgasubs.c,  ***/
  293. /***  has now been merged into a single source              ***/
  294.  
  295. /*******************************************************************/
  296.  
  297. static void _fastcall VCenterDisplay( int nLines )
  298. {
  299. int    lines;
  300. int top, bottom;
  301. long color;
  302.  
  303.     lines  = nLines >> 1;        /* half value of last line 0..x */
  304.     top    = 140 - (lines >> 1);
  305.     bottom = top + lines;
  306.     SetVBorder(